package com.example.device

import android.content.Intent
import android.text.Editable
import androidx.activity.viewModels

import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.device.databinding.ActivityDeviceAddBinding
import com.example.device.entity.DeviceParam
import com.example.device.vm.DeviceViewModel
import com.example.image.glide.GlideImgLoader
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector

import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/device/DeviceAddActivity")
class DeviceAddActivity : BaseMvvmActivity<ActivityDeviceAddBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_device_add
    private val deviceViewModel: DeviceViewModel by viewModels()
    override fun initView() {
        super.initView()
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        //类型选择器
        mDataBinding.ivType.setOnClickListener {
            val list = mutableListOf("门禁设备","监控设备","其它")
            val optionPiker=OptionPicker(this)
            optionPiker.setData(list)
            optionPiker.setTitle("选择工单类型")
            optionPiker.setOnOptionPickedListener { position, item ->
                mDataBinding.edType.text=Editable.Factory.getInstance().newEditable(list[position])
            }
            optionPiker.show()
        }
        //时间选择器
        mDataBinding.ivTime.setOnClickListener {
            val dataPicker=DatePicker(this)
            dataPicker.setTitle("请选择预约日期")
            dataPicker.wheelLayout.setRange(DateEntity.target(1900,1,1), DateEntity.target(2600,12,31),DateEntity.today())
            dataPicker.setOnDatePickedListener { year, month, day ->
                mDataBinding.edTime.text=Editable.Factory.getInstance().newEditable("$year-$month-$day")
            }
            dataPicker.show()
        }
        //添加图片
        mDataBinding.ivIcon.setOnClickListener {
            PictureSelector.create(this,200).selectPicture()
        }
        mDataBinding.btnCommit.setOnClickListener {
            val deviceParam=DeviceParam(
                deviceName = mDataBinding.edContent.text.toString(),
                deviceType = mDataBinding.edType.text.toString(),
                failureRate = 0.7,
                installationLocation = SPUtils.getInstance().getString("gaode").toString(),
                status = "正常",
                id = 0
            )
            deviceViewModel.getAddListData(deviceParam)
        }
        deviceViewModel.mSuccessLiveData.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
        deviceViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==200&&resultCode== RESULT_OK){
            val bean=data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            GlideImgLoader.loadImage(this,bean.path,mDataBinding.ivIcon)
        }
    }
}